library(dplyr)
## make it as a matrix
load("data/relations_meansure/gov_gs_all.RData")

## only focus on 1995-2020
gov_gs_all <- gov_gs_all %>% 
      dplyr::filter(year >=1995)

yearmonths <- sort(unique(gov_gs_all$monthYearEvent))

gdelt_matrix <- list()


for (i in 1:length(yearmonths)){
  slice <- gov_gs_all[which(yearmonths[i]==gov_gs_all$monthYearEvent),] 
  slice <- as.data.frame(slice)
  actors <- sort(unique(unlist(slice[c("codeISOActor1", "codeISOActor2")])))
  smat = matrix(0, nrow=length(actors), ncol=length(actors), dimnames=list(actors, actors))

  # fill in the matrix with matrix indexing on row and column names
  smat[as.matrix(slice[c("codeISOActor1", "codeISOActor2")])] <- slice[["relationscores"]]
  #smat_wt[as.matrix(slice[c("source_iso", "target_iso")])] <- slice[["relationscores_wt"]]
  
  #make it as symetric
  #smat[lower.tri(smat)] = t(smat)[lower.tri(smat)]
  #smat_wt[lower.tri(smat_wt)] = t(smat_wt)[lower.tri(smat_wt)]
  #not define diag
  diag(smat) <- NA
  #diag(smat_wt) <- NA
  
  gdelt_matrix[[i]]=smat
}

names(gdelt_matrix)=yearmonths
save(gdelt_matrix, file = "data/gdelt_matrix.RData")

